using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace SS.core
{
	internal class employee
	{
		private Func objFunc = new Func();
		public employee()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public void loadToListView(ListView lv)
		{
			lv.Items.Clear();
			int num = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT EmpID,EmpName FROM [TabEmployee] ORDER BY [EmpID]";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				lv.Items.Add((num + 1).ToString());
				lv.Items[num].UseItemStyleForSubItems = false;
				lv.Items[num].SubItems.Add(sqlDataReader["EmpID"].ToString());
				lv.Items[num].SubItems.Add(sqlDataReader["EmpName"].ToString());
				num++;
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public void getInfo(string eID, TextBox txtID, TextBox txtDateBirth, TextBox txtName, TextBox txtIDCard, ComboBox cbbGEmployee, TextBox txtEmpResident, TextBox txtEmpAddress, TextBox txtEmail, TextBox txtPhone, TextBox txtNotes)
		{
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			string cmdText = "SELECT * FROM [TabEmployee] WHERE [EmpID] = '" + eID.ToString() + "'";
			SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				txtID.Text = sqlDataReader["EmpID"].ToString();
				txtName.Text = sqlDataReader["EmpName"].ToString();
				txtDateBirth.Text = this.objFunc.DateFormatingToWin(DateTime.Parse(sqlDataReader["EmpDate"].ToString()).ToShortDateString());
				txtEmpResident.Text = sqlDataReader["EmpResident"].ToString();
				txtEmpAddress.Text = sqlDataReader["EmpAddress"].ToString();
				txtIDCard.Text = sqlDataReader["EmpIdCard"].ToString();
				txtPhone.Text = sqlDataReader["EmpPhone"].ToString();
				txtEmail.Text = sqlDataReader["EmpEmail"].ToString();
				txtNotes.Text = sqlDataReader["EmpNotes"].ToString();
				cbbGEmployee.SelectedValue = sqlDataReader["GeID"].ToString();
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
		}
		public int del(string id)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("DelEmployee", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar);
				sqlCommand.Parameters["ID"].Value = id;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
	}
}
